package com.tma.basicfragment.music;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tma.basicfragment.R;

public class SongAdapter extends BaseAdapter {

    private Activity activity;
    private List<Song> data;
    private LayoutInflater inflater;

    private static final int[] THUMBS = { R.drawable.adele, R.drawable.eminem,
            R.drawable.mj, R.drawable.arrehman, R.drawable.alexi_murdoch,
            R.drawable.dido, R.drawable.enrique, R.drawable.ennio,
            R.drawable.rihanna, R.drawable.backstreet_boys };

    public SongAdapter(Activity a, List<Song> songs) {
        activity = a;
        data = songs;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.song_item, null);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.tv_title); 
            holder.artist = (TextView) convertView.findViewById(R.id.tv_artist);
            holder.duration = (TextView) convertView.findViewById(R.id.tv_duration);
            holder.thumbImage=(ImageView) convertView.findViewById(R.id.iv_album_art);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Song song = data.get(position);
 
        holder.thumbImage.setImageResource(THUMBS[position]);
        holder.title.setText(song.getTitle());
        holder.artist.setText(song.getArtist());
        holder.duration.setText(song.getDuration());

        return convertView;
    }
    
    private static class ViewHolder {
        ImageView thumbImage;
        TextView title;
        TextView artist;
        TextView duration;
    }
}
